﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EditoneLib;
using NAudio.Win8.Wave.WaveOutputs;

namespace EditoneLibRT.Utils
{
    /// <summary>
    /// During playback, WASAPI expects at least a stereo stream. If a mono stream is passed, 
    /// it is read as a stereo stream. This utility ensures the channel map has at least 2 
    /// channels if a project only contains mono streams.
    /// </summary>
    /// <exception cref="ArgumentException">Mapping object has nothing mapped.</exception>
    class MappingMonoFix
    {
        internal static void Do(EditoneLib.ChannelMapping channelMapping)
        {
            if (channelMapping.NumChannels > 1) return;
            if (!channelMapping.Any())
            {
                throw new ArgumentException("Mapping object has nothing mapped.");
            }

            var firstMonoTrackInfo = channelMapping[ChannelMapping.All].First();
            var track = firstMonoTrackInfo.Track;
            var volume = firstMonoTrackInfo.Amount;

            channelMapping.Unmap(firstMonoTrackInfo);
            channelMapping.Map(track, 0, 1, volume);
            channelMapping.Map(track, 0, 2, volume);
        }
    }
}
